home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Frameworks / MacZoop 1.6.5 / Basic Classes / Z Sources / ZPrinter.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-30  |  6.2 KB  |  281 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            ObjectMacZapp        -- a standard Mac OOP application template
  5. *
  6. *
  7. *
  8. *            ZPrinter.cpp            -- an object for doing printing
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1996, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21. #include    "ZPrinter.h"
  22. #include    "ZWindow.h"
  23. #include    "MacZoop.h"
  24.  
  25. /*--------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  26.  
  27.  
  28. ZPrinter::ZPrinter( ZWindow* aWindow )
  29. {
  30.     printWindow = aWindow;
  31.     fakeWindow = NULL;
  32.     printInited = FALSE;
  33.     
  34.     // create a handle for the print record. This will be initialsed to
  35.     // the default print settings when the user first chooses Page Setup or Print.
  36.     
  37.     FailNIL( macPrintH = (THPrint) NewHandleClear( sizeof( TPrint )));
  38. }
  39.  
  40. /*---------------------------------***  DESTRUCTOR  ***---------------------------------*/
  41.  
  42. ZPrinter::~ZPrinter()
  43. {
  44.     if ( macPrintH )
  45.         DisposeHandle((Handle) macPrintH );
  46. }
  47.  
  48.  
  49. /*-----------------------------------***  PRINT  ***------------------------------------*/
  50. /*    
  51. print the contents of the current window
  52. ----------------------------------------------------------------------------------------*/
  53.  
  54. void    ZPrinter::Print()
  55. {
  56.     // verify that the window is valid and printable
  57.     
  58.     FailOSErr((printWindow == NULL)? kBadWindowPrintRefErr : noErr );
  59.     FailOSErr( printWindow->IsPrintable()? noErr : kBadWindowPrintRefErr );
  60.     
  61.     try
  62.     {
  63.         SetUpDocTitle( printWindow );
  64.             
  65.         PrOpen();
  66.         FailOSErr( PrError());
  67.  
  68.         SetDefault();
  69.         
  70.         PrintLoop();
  71.         PrClose();
  72.         
  73.         if ( fakeWindow )
  74.             DisposeWindow( fakeWindow );
  75.         fakeWindow = NULL;
  76.     }
  77.     catch( OSErr err )
  78.     {
  79.         PrClose();
  80.         
  81.         if ( fakeWindow )
  82.             DisposeWindow( fakeWindow );
  83.         fakeWindow = NULL;
  84.  
  85.         // show the print problem alert, but do not propagate
  86.         
  87.         Str15    errStr;
  88.         NumToString( err, errStr );
  89.         ParamText( errStr, NULL, NULL, NULL );
  90.         
  91.         StopCursorAnimation();
  92.         
  93.         if (err != userCanceledErr)
  94.             (void) Alert( kInitPrinterAlertID, NULL );
  95.     }
  96. }
  97.  
  98. /*-----------------------------------***  PRINT  ***------------------------------------*/
  99. /*    
  100. print the contents of the window passed
  101. ----------------------------------------------------------------------------------------*/
  102.  
  103. void    ZPrinter::Print( ZWindow* aWindow )
  104. {
  105.     printWindow = aWindow;
  106.     Print();
  107. }
  108.  
  109.  
  110. /*---------------------------------***  PAGESETUP  ***----------------------------------*/
  111. /*    
  112. display the page setup dialog
  113. ----------------------------------------------------------------------------------------*/
  114.  
  115. void    ZPrinter::PageSetUp()
  116. {
  117.     // display the page setup dialog
  118.     
  119.     try
  120.     {
  121.         PrOpen();
  122.         FailOSErr( PrError());
  123.     
  124.         SetDefault();
  125.                 
  126.         StopCursorAnimation();
  127.         PrStlDialog( macPrintH );
  128.         PrClose();
  129.     }
  130.     catch( OSErr err )
  131.     {
  132.         PrClose();
  133.         // show the print problem alert, but do not propagate
  134.         
  135.         Str15    errStr;
  136.         NumToString( err, errStr );
  137.         ParamText( errStr, NULL, NULL, NULL );
  138.  
  139.         if (err != userCanceledErr)
  140.             (void) Alert( kInitPrinterAlertID, NULL );
  141.     }
  142. }
  143.  
  144.  
  145. /*---------------------------------***  PRINTLOOP ***-----------------------------------*/
  146. /*    
  147. display the job dialog and print the range of pages selected
  148. ----------------------------------------------------------------------------------------*/
  149.  
  150. void    ZPrinter::PrintLoop()
  151. {
  152.     Boolean        pgOpen = FALSE;
  153.     short        copies;
  154.     
  155.     // stick up the job dialog so the user can set copies, start, end, etc.
  156.     
  157.     StopCursorAnimation();
  158.     
  159.     if ( PrJobDialog( macPrintH ))
  160.     {
  161.         // user OK'd the dialog, so set the page ranges and start printing them.
  162.         // set up the paper rect
  163.         SetWatchCursor();
  164.         paperRect = (*macPrintH)->prInfo.rPage;
  165.         
  166.         // calculate pagination. The window does this based on its content area and
  167.         // the paper area passed to it. Default pagination tiles first horizontally
  168.         // then vertically. pH and pV will be at least 1.
  169.         
  170.         printWindow->CalcPages( paperRect, &pH, &pV );
  171.     
  172.         pageStart = (*macPrintH)->prJob.iFstPage;
  173.         pageEnd = (*macPrintH)->prJob.iLstPage;
  174.     
  175.         // make sure the end page is sensible to the actual number of pages
  176.         // to print.
  177.         
  178.         if (pageEnd > (pH * pV))
  179.             pageEnd = (pH * pV);
  180.             
  181.         if (pageStart < 1)
  182.             pageStart = 1;
  183.             
  184.         // make the magic fix for mac happiness:
  185.             
  186.         (*macPrintH)->prJob.iFstPage = 1;
  187.         (*macPrintH)->prJob.iLstPage = 9999;
  188.         
  189.         // print the requested number of copies
  190.                 
  191.         for( copies = 0; copies < (*macPrintH)->prJob.iCopies; copies++ )
  192.         {
  193.             // if page start is later than page end, nothing to print (sanity check)
  194.         
  195.             if ( pageStart <= pageEnd )
  196.             {
  197.                 printPort = PrOpenDoc( macPrintH, NULL, NULL );
  198.                 FailOSErr( PrError());
  199.                 
  200.                 try
  201.                 {
  202.                     printWindow->PrintingStarting();
  203.                     
  204.                     // for each page, open the page and print it
  205.  
  206.                     for (pageNum = pageStart; pageNum <= pageEnd; pageNum++ )
  207.                     {
  208.                         PrOpenPage( printPort, NULL );
  209.                         FailOSErr( PrError());
  210.                         
  211.                         pgOpen = TRUE;
  212.                         
  213.                         // call the window to do the relevant rendering, etc.
  214.                         
  215.                         printWindow->PrintOnePage( pageNum, paperRect );
  216.                 
  217.                         PrClosePage( printPort );
  218.                         FailOSErr( PrError());
  219.                         
  220.                         pgOpen = FALSE;
  221.                     }
  222.                     
  223.                     // the printing has finished, so inform the window
  224.                     
  225.                     printWindow->PrintingFinishing();
  226.                 }
  227.                 catch( OSErr err )
  228.                 {
  229.                     if ( pgOpen )
  230.                         PrClosePage( printPort );
  231.                         
  232.                     PrCloseDoc( printPort );
  233.                     
  234.                     throw err;
  235.                 }
  236.                 
  237.                 PrCloseDoc( printPort );
  238.                 
  239.                 FailOSErr( PrError());
  240.                 
  241.                 // we may need to spool the file, so check the spool flag
  242.                 
  243.                 TPrStatus  prStatus;
  244.                 
  245.                 if ((*macPrintH)->prJob.bJDocLoop == bSpoolLoop )
  246.                 {
  247.                     PrPicFile( macPrintH, NULL, NULL, NULL, &prStatus);
  248.                     FailOSErr( PrError());
  249.                 }
  250.             }
  251.         }
  252.     }
  253. }
  254.  
  255.  
  256.  
  257. void    ZPrinter::SetUpDocTitle( ZWindow* aWindow )
  258. {
  259.     // mac print driver assums top window is the one we are printing. This is not true if we
  260.     // have floaters, so to make sure, we create a temporary front window hidden behind the
  261.     // menubar and make sure its title is set to the doc we are printing.
  262.     
  263.     Rect    r = { 0, 0, 3, 3 };
  264.     Str31    title;
  265.     
  266.     aWindow->GetName( title );
  267.     fakeWindow = NewWindow( NULL, &r, title, TRUE, 0, (WindowPtr) -1L, FALSE, 0);
  268.     SelectWindow( fakeWindow );
  269. }
  270.  
  271.  
  272. void    ZPrinter::SetDefault()
  273. {
  274.     if ( ! printInited )
  275.     {
  276.         PrintDefault( macPrintH );
  277.         
  278.         printInited = TRUE;
  279.     }
  280. }
  281.